home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / PAS_0493 / STRFIELD.PAS < prev    next >
Pascal/Delphi Source File  |  1993-04-22  |  3KB  |  104 lines

  1. {─ Fido Pascal Conference ────────────────────────────────────────────── PASCAL ─
  2. Msg  : 505 of 536
  3. From : Bernie Pallek                       1:247/128.0          21 Apr 93  18:26
  4. To   : Stephen Cheok
  5. Subj : STARFIELD
  6. ────────────────────────────────────────────────────────────────────────────────
  7.  SC>         Hmm.. does anyone have an example of a
  8.  SC> starfield routine in Turbo Pascal.. or can be used
  9.  SC> with Turbo Pascal.. using the ASSEMBLER..  I want to
  10.  SC> try to make one.. but I need some help with it.
  11.  
  12. OK, here's a sample (I don't know what kind of starfield you're looking for):}
  13.  
  14. {EGA/VGA parallax stars}
  15.  
  16. USES Crt, Graph;
  17.  
  18. CONST
  19.      starCol : ARRAY[0..2] OF Byte = (8, 7, 15);
  20.  
  21. TYPE
  22.     StarRec = RECORD
  23.               x : Integer;
  24.               y : Integer;
  25.               d : Integer;  { depth }
  26.               END;
  27. VAR
  28.    stars : ARRAY[0..31] OF StarRec;
  29.    xinc,
  30.    yinc  : Integer;
  31.    ch    : Char;
  32.  
  33.  
  34. PROCEDURE OpenGraph;
  35. VAR
  36.    gd, gm : Integer;
  37. BEGIN
  38.      DetectGraph(gd, gm);
  39.      { this doesn't care if you don't have correct video card or not }
  40.      InitGraph(gd, gm, 'C:\BP\BGI');      { put the path to your BGI }
  41. END;
  42.  
  43. PROCEDURE InitStars;
  44. VAR
  45.    i : Integer;
  46. BEGIN
  47.      FOR i := 0 TO 31 DO WITH stars[i] DO BEGIN
  48.          x := Random(GetMaxX);
  49.          y := Random(GetMaxY);
  50.          d := Random(3);
  51.      END;
  52. END;
  53.  
  54. PROCEDURE MoveStars;
  55. VAR
  56.    i : Integer;
  57. BEGIN
  58.      FOR i := 0 TO 31 DO WITH stars[i] DO BEGIN
  59.          PutPixel(x, y, 0);
  60.          x := x + xinc * (d + 1);
  61.          IF (x < 0) THEN x := x + GetMaxX;
  62.          IF (x > GetMaxX) THEN x := x - GetMaxX;
  63.          y := y + yinc * (d + 1);
  64.          IF (y < 0) THEN y := y + GetMaxY;
  65.          IF (y > GetMaxY) THEN y := y - GetMaxY;
  66.          PutPixel(x, y, starCol[d]);
  67.      END;
  68. END;
  69.  
  70.  
  71. BEGIN
  72.      OpenGraph;  (* enter graphics mode *)
  73.      InitStars;
  74.      xinc := 1;
  75.      yinc := 0;
  76.      REPEAT
  77.            MoveStars;
  78.            (* delay here for faster computers *)
  79.      UNTIL KeyPressed;
  80.      ch := ReadKey;
  81.      IF (ch = #0) THEN ch := ReadKey;  (* get rid of extended keycodes *)
  82.      CloseGraph;
  83. END.
  84.  
  85. ------------SNIPLINE-----------
  86.  
  87. Whew!  There you have it!  Untested, of course, so you may have to iron out a
  88. few bugs.
  89.  
  90. **** BIG HINT: You should probably use REAL numbers instead of INTEGER numbers
  91. for x and y positions AND increments, and Round them when PutPixel-ing!  This
  92. will allow you to make smoother transitions, as well as bouncing effects, and
  93. other neat stuff. ****
  94.  
  95. You'll notice (if the thing works) that the stars move horizontally only, and
  96. the dimmer ones move slower than the bright ones (parallax/multi-layered).  You
  97. can add extra layers, but remember to change the StarCol constant so you have
  98. the right number of colours for the stars.
  99.  
  100. Sorry, I was too lazy to comment it thoroughly; I'm expecting that you'll be
  101. able to figure it out without too much trouble.  Sorry if you can't; write me
  102. for an explanation.  TTYL.
  103.  
  104. Bernie.